﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using OutdoorLib.BaseEntity.Face;
using Grishko.DataTier;

public partial class Ctl_ctlContactFaces : System.Web.UI.UserControl
{
    /// <summary>
    /// Источник даных контрола
    /// </summary>
    public Func<EntityList> DataSource = null;
    public Func<ContactFace,bool> OnRemoveClick = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        MyInit();
    }

    private void MyInit()
    {

        //привязать событие открыть закрыть lvContactFace
        if (lvContactFaces.EditItem != null)
        {

            Ctl_ctlContactFace r1 = (Ctl_ctlContactFace)lvContactFaces.EditItem.FindControl("ctlContactFace2");
            r1.FaceSource = ctlContactFace_FaceSource;
            r1.OnCancelClick = ctlContact_OnCancelClick;
            r1.OnSaveClick = ctlContact_OnCancelClick;
          
        }
    }
    protected ContactFace ctlContactFace_FaceSource(string ID)
    {
        return (ContactFace)DataSource()[ID];
    }

    protected bool  ctlContact_OnCancelClick()
    {
        lvContactFaces.EditIndex = -1;
        DataBind();
        return true;
    }

    

    protected  string GetCompanyName(ContactFace Face)
    {

        return "Имя компании";
    }


    protected bool OnContactFaceClick()
    {
        lvContactFaces.EditIndex = -1;
        DataBind ();
        return true;
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        if (null == DataSource)
            return;

        lvContactFaces.DataSource = DataSource();
        lvContactFaces.DataBind();
    }

    //int oldRowCount = 0;
    //ContactFace[] rs = null;
    //protected ContactFace[] GetList(int startRowIndex, int maximumRows, out int RowCount)
    //{
    //    //if (rs != null) { RowCount = oldRowCount; return rs; }

    //    //wsFace ws = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
    //    //ListWhere w = (ListWhere)Where;

    //    //w.ListLoadStatment.StartRowIndex = startRowIndex;
    //    //w.ListLoadStatment.EndRowIndex = maximumRows;
    //    //ContactFaceList l = ws.GetContactFaceList((ContactFaceList.sqlLoadStatment)w.ListLoadStatment);
    //    //oldRowCount = RowCount = l.OutputRwCnt;
        
    //    //////////заряжаем comparer функцией сортировки 
    //    //rs = l.ToArray<ContactFace>();
    //    //Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;        
    //    //Array.Sort(rs, Where.SortCondition);
    //    //return rs;
    //}

    ///// <summary>
    ///// Лексема
    ///// </summary>
    //public string Lexeme { get { return Where.ListLoadStatment.Lexeme; } set { Where.ListLoadStatment.Lexeme = value; } }

    ///// <summary>
    ///// Набор пар = параметр значение, сформировать запрос к 
    ///// Коллекция - vPropertyOfList.ParamListItem
    ///// </summary>       
    //public EntityList ParamValueList
    //    { get { return Where.ListLoadStatment.ParamValueList; }}


    
    //public ListWhere Where
    //{
    //    get
    //    {
    //        string key = "Where" + KeyPref;
    //        if (!CacheTools.IsSessionCacheObject(key))
    //        {
    //            ListWhere r = new ListWhere();                
    //            r.ListLoadStatment = new ContactFaceList.sqlLoadStatment();

    //            r.SortCondition.SortDirection = SortDirection.Ascending;
    //            r.SortCondition.SortField = "FirstName";
    //            CacheTools.SetSessionCacheObject(key, r);
    //        }
    //        return (ListWhere)CacheTools.GetSessionCacheObect(key);
    //    }

    //    set
    //    {
    //        string key = "Where" + KeyPref;
    //        CacheTools.ResetSessionCache(key);
    //        CacheTools.SetSessionCacheObject(key, value);
    //    }

    //}

    /// <summary>
    /// префикс - для хранения в кэше 
    /// </summary>
    public string KeyPref { get { return hfPref.Value + HttpContext.Current.User.Identity.Name; } set { hfPref.Value = value; } }

    protected void BntRemoveWorker_Click(object sender, EventArgs e)
    {
        if (null ==OnRemoveClick )
            return ;               

        ImageButton btn =(ImageButton) sender ;
        ContactFace f=(ContactFace) DataSource()[btn.CommandArgument];
        if(null!=f)
                OnRemoveClick(f);
        
    }

    protected void ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Edit":
                {

                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lvContactFaces.EditIndex = d.DataItemIndex;
                    break;
                }
            case "Update":
                {
                    lvContactFaces.EditIndex = -1;
                    break;
                }

        }
            this.DataBind();
    }

    protected void ItemEditing(object sender, ListViewEditEventArgs e)
    {
        MyInit();
    }
        
    protected void ItemDataBound(object sender, ListViewItemEventArgs e)
    {
      

    }
    
    protected void ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
    }

    public int PageSize
    {
        get { return Convert.ToInt16(hfPageSize.Value); }
        set { hfPageSize.Value = value.ToString(); }
    }
   
    public bool PagerVisible 
    {
        get { return true; }
    }

    protected void lvContactFaces_PagePropertiesChanged(object sender, EventArgs e)
    {
        DataBind();
    }

    protected string GetHeadCss(string HearTd)
    {
        return "";
        //if (Where.SortCondition.SortField != HearTd) return string.Empty;
        //return "sort" + Where.SortCondition.SortDirection.ToString();
    }

    protected void lvSort(object sender, EventArgs e)
    {
        //lvContactFaces.EditIndex = -1;
        //string s = ((LinkButton)sender).CommandArgument;
        //Where.SortCondition.SortDirection = (Where.SortCondition.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending); ;
        //Where.SortCondition.SortField = s;
        //DataBind();
    }
    private bool _ShowHead = false;
    public bool ShowHead { get { return _ShowHead; } set { _ShowHead = value; } }
}
